package com.dc.bobaoge.project.base.core.mq;

import com.alibaba.fastjson2.JSON;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;

/**
 * RabbitMqUtil
 *
 * @author chenxueli
 * @date 2024-01-16 10:58:00
 */
@Component
public class RabbitMqUtil {

    private static final MessageProperties PROPERTIES = new MessageProperties();

    static {
        PROPERTIES.setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN);
    }

    @Resource
    private RabbitTemplate rabbitTemplate;

    /**
     * 同步信息
     *
     * @param id      消息id
     * @param quarter 季度
     * @param type    消息类型
     */
    public void syncMessage(Long id, String quarter, String type) {
        var map = new HashMap<String, Object>();
        map.put("id", id);
        map.put("quarter", quarter);
        map.put("type", type);
        var content = JSON.toJSONString(map);
        rabbitTemplate.send("message.sync.queue", new org.springframework.amqp.core.Message(content.getBytes(StandardCharsets.UTF_8), PROPERTIES));
    }

    /**
     * 同步留言次数的方法
     */
    public void addCommentCount(Long messageId, String quarter, String type) {
        // 发送增加次数的方法
        var map = new HashMap<String, Object>();
        map.put("id", messageId);
        map.put("type", type);
        map.put("quarter", quarter);
        map.put("comment", 1);
        var content = JSON.toJSONString(map);
        rabbitTemplate.send("message.sync.count.queue", new org.springframework.amqp.core.Message(content.getBytes(StandardCharsets.UTF_8), PROPERTIES));
    }

}
